package com.ruoyi.system.wnjk.test.rabbitmq.dead;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.ruoyi.system.wnjk.test.rabbitmq.utils.RabbitMqUtils;

import java.util.HashMap;
import java.util.Map;

public class Consumer01 {
    private static final String NORMAL_EXCHANGE = "normal_exchange";    // 普通交换机名称
    private static final String DEAD_EXCHANGE = "dead_exchange";    // 死信交换机名称
    private static final String NORMAL_QUEUE = "normal_queue";  // 普通队列名称
    private static final String DEAD_QUEUE = "dead_queue";  // 死信队列名称

    public static void main(String[] args) throws Exception {
//        // 创建channel
//        Channel channel = RabbitMqUtils.getChannel();
//        // 声明普通和死信交换机(类型都为DIRECT)
//        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//        channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//        // 声明普通和死信队列(普通队列需要传递参数设置死信交换机及其对应的路由key)
//        Map<String, Object> arguments = new HashMap<>();
//        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信交互机
//        arguments.put("x-dead-letter-routing-key", "lisi"); // 设置与死信交换机间的routing-key
//        channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
//        channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//        // 绑定队列与交换机,设置其间的路由key
//        channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
//        channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
//        // 消费消息
//        System.out.println("Consumer01等待接收普通队列到消息......");
//        DeliverCallback deliverCallback = (consumerTag, message) -> {
//            System.out.println("Consumer01接收到普通队列的消息:" + new String(message.getBody()));
//        };
//        CancelCallback cancelCallback = (consumerTag) -> {
//            System.out.println("消息消费被中断");
//        };
//        channel.basicConsume(NORMAL_QUEUE, deliverCallback, cancelCallback);

//        //限制最大长度
//        // 创建channel
//        Channel channel = RabbitMqUtils.getChannel();
//        // 声明普通和死信交换机(类型都为DIRECT)
//        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//        channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//        // 声明普通和死信队列(普通队列需要传递参数设置死信交换机及其对应的路由key)
//        Map<String, Object> arguments = new HashMap<>();
//        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信交互机
//        arguments.put("x-dead-letter-routing-key", "lisi"); // 设置与死信交换机间的routing-key
//        arguments.put("x-max-length", 6);   // 设置正常队列长度为6
//        channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
//        channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//        // 绑定队列与交换机,设置其间的路由key
//        channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
//        channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
//        // 消费消息
//        System.out.println("Consumer01等待接收普通队列到消息......");
//        DeliverCallback deliverCallback = (consumerTag, message) -> {
//            System.out.println("Consumer01接收到普通队列的消息:" + new String(message.getBody()));
//        };
//        CancelCallback cancelCallback = (consumerTag) -> {
//            System.out.println("消息消费被中断");
//        };
//        channel.basicConsume(NORMAL_QUEUE, deliverCallback, cancelCallback);


        // 创建channel
        Channel channel = RabbitMqUtils.getChannel();
        // 声明普通和死信交换机(类型都为DIRECT)
        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
        // 声明普通和死信队列(普通队列需要传递参数设置死信交换机及其对应的路由key)
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信交互机
        arguments.put("x-dead-letter-routing-key", "lisi"); // 设置与死信交换机间的routing-key
        channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
        channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
        // 绑定队列与交换机,设置其间的路由key
        channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
        channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
        // 消费消息
        System.out.println("Consumer01等待接收普通队列到消息......");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String mes = new String(message.getBody(), "UTF-8");
            if (mes.equals("5")) {
                System.out.println("Consumer01接收到普通队列的消息" + mes + "并拒收该消息");
                // 第二个参数requeue设置为false,代表拒绝重新入队,也就是该队列如果配置了死信交换机将发送到死信队列中
                channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
            } else {
                System.out.println("Consumer01接收到普通队列的消息:" + new String(message.getBody()));
                channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
            }
        };
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息消费被中断");
        };
        // 开启手动应答
        boolean autoAck = false;
        channel.basicConsume(NORMAL_QUEUE, autoAck, deliverCallback, cancelCallback);

    }
}
